/*
 * This file is part of aKoustiK.

 * aKoustiK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * aKoustiK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with aKoustiK.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <KGlobal>
#include <KSharedConfig>
#include <KSharedConfigPtr>

#include <QtCore/QDebug>
#include <QtGui/QHeaderView>
#include <QtGui/QKeyEvent>

#include <kore.h>
#include <ui/player/playlistview.h>
#include <ui/player/playlistitemdelegate.h>

PlaylistView::PlaylistView(QWidget *parent) : QTreeView(parent) {
    // connect signals:
    connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(userDoubleClicked(QModelIndex)));
    connect(this, SIGNAL(playItem(int)), Kore::instance().player(), SLOT(play(int)));
}

PlaylistView::~PlaylistView() {
    // TODO we should change when it's resized instead of destructor
    QHeaderView* head = header();
    QList<int> list;
    for (int i = 0; i < head->count(); i++) {
        list.append(head->sectionSize(i));
    }
    KGlobal::config()->group("PlaylistView").writeEntry("Columns", list);
}

void PlaylistView::keyPressEvent(QKeyEvent *event) {
    // TODO can't understand why selectAll dies not work
    /*
    if (event->matches(QKeySequence::SelectAll)) {
        qDebug("PlaylistView: selectAll pressed");
        selectAll();
        return;
    }
    */
    switch(event->key()) {
    case Qt::Key_Return:
    case Qt::Key_Enter:
    case Qt::Key_Space:
        {
            // activation key pressed, reacting:
            emit doubleClicked(currentIndex());
            return;
        }
    case Qt::Key_Delete:
        {
            // delete operations:
            QModelIndexList selectedList = selectedIndexes();
            if (!selectedList.isEmpty()) {
                emit deleteItems(selectedList);
            }
            return;
        }
    default: QTreeView::keyPressEvent(event);
    }
}

void PlaylistView::setModel(QAbstractItemModel *model) {
    QTreeView::setModel(model);
    // we should load column widths now, because in constructor there are no columns yet:
    qDebug() << "PlaylistView: loading column widths...";
    const QList<int>& list = KGlobal::config()->group("PlaylistView").readEntry("Columns", QList<int>());
    if (!list.isEmpty()) {
        qDebug() << "PlaylistView: saved column widths found...";
        QHeaderView* head = header();
        int max = head->count();
        qDebug("PlaylistView: header has %i columns.", max);
        if (list.size() < max) {
            max = list.size();
        }
        for (int i = 0; i < head->count(); i++) {
            head->resizeSection(i, list.at(i));
        }
    }
}

void PlaylistView::userDoubleClicked(const QModelIndex &index) {
    if (index.isValid()) {
        emit playItem(index.row());
    }
}
